package com.bjx.generator.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.generator.domain.entity.TableSonEntity;
import com.bjx.generator.domain.vo.TableSonVO;
import com.bjx.generator.mapper.TableSonMapper;
import com.bjx.generator.service.ITableSonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: 主子关系服务
 * @author: liushuai
 * @date: 2022/9/22
 */
@Service
public class TableSonServiceImpl extends ServiceImpl<TableSonMapper, TableSonEntity> implements ITableSonService {

    @Resource
    private TableSonMapper tableSonMapper;


    @Override
    public void saveTableSonBatchByTableId(List<TableSonVO> entityList, Long tableId) {
        LambdaQueryWrapper<TableSonEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(TableSonEntity::getTableId,tableId);
        tableSonMapper.delete(wrapper);
        if(CollectionUtil.isNotEmpty(entityList)){
            List<TableSonEntity> entities = new ArrayList<TableSonEntity>();
            entityList.forEach(res->{
                res.setTableId(tableId);
                res.setTableSonId(null);
                TableSonEntity entity = Convert.convert(TableSonEntity.class, res);
                entities.add(entity);

            });
            this.saveBatch(entities);
        }

    }

    @Override
    public List<TableSonVO> listTableSonByTableId(Long tableId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("a.table_id",tableId);
        List<TableSonVO> vos = tableSonMapper.listTableSonByTableId(wrapper);
        return vos;
    }

    @Override
    public void deleteTableSonByTableId(Long tableId) {
        LambdaQueryWrapper<TableSonEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(TableSonEntity::getTableId,tableId);
        tableSonMapper.delete(wrapper);
    }
}
